package org.activequant.data.util;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Iterator that is built on top of a List and iterates in reverse list order.
 * Iterator does not implement {@link #remove()} method.
 * <br/>
 * ReverseListIterator&lt;T&gt; implements Iterator&lt;T&gt;. Holds the following associated variables:
 * <ul>
 * <li>series(List&lt;T&gt;)</li>
 * <li>index(int)</li>
 * </ul>
 * <b>History:</b><br>
 *  - [03.12.2007] Created (Mike Kroutikov)<br>
 *
 *  @author Mike Kroutikov
 */
public class ReverseListIterator<T> implements Iterator<T> {

	private final Logger log = Logger.getLogger(getClass());
	/**
	 * private final List&lt;T&gt; series;
	 */
	private final List<T> series;
	/**
	 * private int index;
	 */
	private int index;
	/**
	 * Constructs a ReverseListIterator&lt;T&gt;(implements Iterator&lt;T&gt;) using the given s(List&lt;T&gt;) to
	 * set its associated series(List&lt;T&gt;.<br/>
	 * Sets its associated index(int) with the size of the given s(List&lt;T&gt;)
	 * @param s
	 */
	public ReverseListIterator(List<T> s) {
		series = s;
		index = series.size();
		//log.info("reverse iterator initialized with series size=" + index);
	}
	/**
	 * returns whether the associated index(int) is greater than 0
	 */
	public boolean hasNext() {
		return index > 0;
	}
	/**
	 * if the associated index(int) is greater than 0, it decreases it by 1 and then returns the element at that position/index
	 * from the associated series(List&lt;T&gt;). Otherwise, it returns null.
	 */
	public T next() {
		if(hasNext()) {
			return series.get(--index);
		}
		return null;
	}
	/**
	 * throws an UnsupportedOperationException()
	 */
	public void remove() {
		throw new UnsupportedOperationException();
	}
}